cc.Class({
	extends: cc.Component,

	properties: {
		node_playerInfo: cc.Node,
		node_cardList: cc.Node,
		sp_sex: [cc.SpriteFrame],
		icon_specialtype: [cc.SpriteFrame],
		prefab_specialtype: cc.Prefab,
	},
	SetPosInfo: function (data) {
		this.CreateSpecialTypeNode();
		for (let i = 0; i < this.node_cardList.children.length; i++) {
			let cardNode = this.node_cardList.children[i];
			cardNode.active = false;
		}
		this.SetPlayerInfo(data);

	},
	SetPlayerInfo: function (playerInfo) {
		let head = this.node_playerInfo.getChildByName("head");
		let lb_name = this.node_playerInfo.getChildByName("lb_name");
		let lb_id = this.node_playerInfo.getChildByName("lb_id");
		let sp_sex = this.node_playerInfo.getChildByName("sp_sex");
		let pid = playerInfo["pid"];

		this.ShowHead(head, pid);

		this.GetLabelByNode(lb_name).string = playerInfo["name"];
		this.GetLabelByNode(lb_id).string = "ID:" + app[app.subGameName + "_ComTool"]().GetPid(pid);
		this.GetSpriteByNode(sp_sex).spriteFrame = this.sp_sex[playerInfo["sex"]];
		// this.ShowPointValue(playerInfo["roomScore"]);
		// this.ShowPointValue(playerInfo["score"]);
		this.ShowPointValue(playerInfo["allScore"]);
		let cardList = this.GetCardList(playerInfo["cardList"]);
		this.ShowPrivateCards(cardList);
		this.ShowQiPai(playerInfo["isQiPai"]);
		this.ShowSpecialType(playerInfo["rewards"]);
	},
	ShowHead: function (head, pid) {
		let WeChatHeadImage = head.getComponent(app.subGameName + "_WeChatHeadImage");
		WeChatHeadImage.OnLoad();
		WeChatHeadImage.ShowHeroHead(pid);
	},
	ShowPrivateCards: function (privateCards) {
		let cardList = this.node_cardList;
		for (let i = 0; i < privateCards.length; i++) {
			let cardValue = privateCards[i];
			let cardNode = cardList.children[i];
			cardNode.active = true;
			this.ShowCard(cardValue, cardNode, true);
		}

	},
	ShowQiPai: function (isQiPai) {
		this.node.getChildByName("img_qp").active = isQiPai;
	},
	ShowSpecialType: function (rewards) {
		let wndPath = "cardtypelayout/";
		for (let i = 0; i < rewards.length; i++) {
			let reward = rewards[i];
			this.SetWndProperty(wndPath + reward, "active", true);
		}
	},
	ShowPointValue: function (setPoint) {
		let lb_point_win = this.node.getChildByName("lb_point_win");
		let lb_point_lose = this.node.getChildByName("lb_point_lose");
		if (setPoint > 0) {
			this.GetLabelByNode(lb_point_win).string = "+" + setPoint;
			lb_point_win.active = true;
			lb_point_lose.active = false;
		} else {
			this.GetLabelByNode(lb_point_lose).string = "+" + setPoint;
			lb_point_lose.active = true;
			lb_point_win.active = false;
		}
	},
	ShowCard: function (cardValue, cardNode, isShowIcon1) {
		cardNode.active = true;
		let realValue = 0;
		if (cardValue > 500) {
			realValue = cardValue - 500;
		}
		else {
			realValue = cardValue;
		}
		cardNode.active = true;
		if (cardValue == 0) {
			cardNode.getChildByName("poker_back").active = true;
			return;
		} else {
			cardNode.getChildByName("poker_back").active = false;
		}
		app[app.subGameName.toUpperCase()+"PokerCard"]().GetPokeCard(realValue, cardNode, isShowIcon1, true);
	},
	CreateSpecialTypeNode: function () {
		let cardtypelayout = this.node.getChildByName("cardtypelayout");
		cardtypelayout.removeAllChildren();
		let name = 20;
		for (let j = 0; j < 12; j++) {
			let cardNode = cc.instantiate(this.prefab_specialtype);
			cardNode.name = name + "";
			cardNode.getComponent(cc.Sprite).spriteFrame = this.icon_specialtype[j];
			cardtypelayout.addChild(cardNode);
			cardNode.active = false;
			name++;
		}
	},
	GetCardList: function (dunPos) {
		console.log("GetCardList", dunPos);
		let cardList = [];
		for (let key in dunPos) {
			let cards = dunPos[key];
			let sortCards = [];
			if (cards.length == 0) {
				sortCards = [0, 0, 0];
			} else {
				sortCards = this.GetSortCard(cards);
			}
			for (let i = 0; i < sortCards.length; i++) {
				cardList.push(sortCards[i]);
			}
		}
		console.log("GetCardList", cardList);
		return cardList;
	},
	GetSortCard: function (cards) {
		let array = cards;
		app[app.subGameName.toUpperCase()+"LogicGame"]().SortCardByMin(cards);
		//23A 667 566
		if (app[app.subGameName.toUpperCase()+"LogicGame"]().GetCardValue(array[0]) == 2 &&
			app[app.subGameName.toUpperCase()+"LogicGame"]().GetCardValue(array[1]) == 3 &&
			app[app.subGameName.toUpperCase()+"LogicGame"]().GetCardValue(array[2]) == 14) {
			array.unshift(cards[2]);
			array.pop();
		}
		if (app[app.subGameName.toUpperCase()+"LogicGame"]().GetCardValue(array[0]) < app[app.subGameName.toUpperCase()+"LogicGame"]().GetCardValue(array[1]) &&
			app[app.subGameName.toUpperCase()+"LogicGame"]().GetCardValue(array[1]) == app[app.subGameName.toUpperCase()+"LogicGame"]().GetCardValue(array[2])) {
			array.reverse();
		}
		return array;
	},
	GetLabelByNode: function (node) {
		let label = node.getComponent(cc.Label);
		return label;
	},
	GetSpriteByNode: function (node) {
		let sprite = node.getComponent(cc.Sprite);
		return sprite;
	},
	//设置控件属性
	SetWndProperty:function(wndPath, property, value){

		let wndNode = cc.find(wndPath, this.node);
		if(!wndNode){
			console.error("SetWndProperty(%s,%s,%s) not find wndPath", wndPath, property, value);
			return
		}

		let wndComponent = null;
		if(property == "text"){
			wndComponent = wndNode.getComponent(cc.Label);
			//可能是label
			if(wndComponent){
			}
			//没有label可能是editbox
			else if(wndNode.getComponent(cc.RichText)){
				wndComponent = wndNode.getComponent(cc.RichText);
			}
			else{
				wndComponent = wndNode.getComponent(cc.EditBox);
			}

			if(!wndComponent){
				console.error("SetWndProperty(%s,%s,%s) wnd not find cc.Label cc.EditBox", wndPath, property, value);
				return
			}
			wndComponent.string = value + "";
		}
		else if(property == "position"){
			wndNode.setPosition(value)
		}
		else if(property == "active"){
			wndNode.active = value;
		}
		else if(property == "color"){
			wndNode.color = value
		}
		else if(property == "image"){
			this.SetWndImage(wndPath, value);
		}
		else if(property == "scale"){
			wndNode.scale = value
		}
		else if(property == "opacity"){
			wndNode.opacity = value
		}
		else if(property == "interactable"){
			wndComponent = wndNode.getComponent(cc.Button);
			if(!wndComponent){
				console.error("SetWndProperty(%s,%s,%s) wnd not find cc.Button", wndPath, property, value);
				return
			}
			wndComponent.interactable = value;
		}
		else if(property == "progress"){
			wndComponent = wndNode.getComponent(cc.ProgressBar);
			if(!wndComponent){
				console.error("SetWndProperty(%s,%s,%s) wnd not find cc.ProgressBar", wndPath, property, value);
				return
			}
			wndComponent.progress = value;
		}
		else{
			console.error("SetWndProperty(%s,%s,%s) property error", wndPath, property, value);
			return
		}
	},
});